﻿using System.Collections;
using System.Collections.Generic;
using FairyGUI;
using UnityEngine;

public class FInputPanelMediator : MyWindow
{
    TwoDelegate _callBack;
    List<GObject> _inputValBoxList;

    protected override void OnInit()
    {
 	    base.OnInit();
        this.contentPane = UIPackage.CreateObject("CommonPackage", "InputPanel").asCom;
        this.Center();//居中
        this.modal = true;

        this.contentPane.GetChild("closeBtn").onClick.Add(OnCloseBtn);
        var submitBtn = this.contentPane.GetChild("submitBtn");
        var cancelBtn = this.contentPane.GetChild("cancelBtn");
        submitBtn.onClick.Add(OnSubmitBtn);
        cancelBtn.onClick.Add(OnCloseBtn);

        _inputValBoxList = new List<GObject>();
        _inputValBoxList.Add(this.contentPane.GetChild("inputValBox1"));
    }
    public void InitData(string val,TwoDelegate cb)
    {
        var valArr = val.Split('-');
        int num = valArr.Length;
        if (num < 1) return;
        _callBack = cb;
        var totalNum = _inputValBoxList.Count;
        //移除多余的
        for (int j = 0;j < totalNum - num; j++)
        {
            this.contentPane.RemoveChild(_inputValBoxList[_inputValBoxList.Count - 1]);
            _inputValBoxList.RemoveAt(_inputValBoxList.Count - 1);
        }
        totalNum = _inputValBoxList.Count;
        //补充不够的
        for (int i = totalNum; i < num; i++)
        {
            var obj = UIPackage.CreateObject("CommonPackage", "InputValBox");
            obj.name = "inputValBox" + i + 1;
            ((GComponent)obj).GetChild("title").text = "值" + (i + 1) + "：";
            this.contentPane.AddChild(obj);
            var inputGroup = this.contentPane.GetChild("inputGroup").asGroup;
            obj.group = inputGroup;
            obj.x = _inputValBoxList[0].x;
            _inputValBoxList.Add(obj);
        }
        //清除之前的值
        for (int i = 0; i < _inputValBoxList.Count; i++)
        {
            ((GComponent)_inputValBoxList[i]).GetChild("editBox").text = valArr[i];
        }
    }
	// Use this for initialization
    protected override void OnShown()
    {
 	    base.OnShown();
         this.Center();//居中   
    }
    private void OnSubmitBtn(EventContext context)
    {
        if (_callBack != null)
        {
            var str = GetValStr();
            Debug.Log(str);
            if (str == "") return;
            _callBack(1, str);
        }
        this.Hide();
    }
    private void OnCloseBtn(EventContext context)
    {
        if (_callBack != null) _callBack(0,"");
        this.Hide();
    }
    private string GetValStr()
    {
        string str = "";
        for(int i = 0;i < _inputValBoxList.Count; i++)
        {
            var val = ((GComponent)_inputValBoxList[i]).GetChild("editBox").text;
            if (val == "0") ((GComponent)_inputValBoxList[i]).GetChild("editBox").text = "";
            if (val == "" || val == "0") return "";
            if(str == val) continue;
            if (str != "") str += "-";
            str = str + val;
        }
        return str;
    }
}
